package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.mapper.StrategyCatalogMapper;
import cn.wolfcode.wolf2w.business.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.business.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.api.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.business.vo.StrategyCatalogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 攻略分类Service业务层处理
 * 
 * @author 浪兎兎
 * @date 2025-05-30
 */
@Service
@Transactional
public class StrategyCatalogServiceImpl extends ServiceImpl<StrategyCatalogMapper,StrategyCatalog> implements IStrategyCatalogService {

    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public IPage<StrategyCatalog> queryPage(StrategyCatalogQuery qo) {
        IPage<StrategyCatalog> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyCatalog> catalogList(Long destId) {
        List<StrategyCatalog> list = lambdaQuery().eq(StrategyCatalog::getDestId, destId).list();
        list.forEach(catalog -> {
            LambdaQueryWrapper<Strategy> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Strategy::getCatalogId, catalog.getId());
            List<Strategy> strategies = strategyMapper.selectList(wrapper);
            catalog.setStrategies(strategies);
        });
        return list;
    }

    @Override
    public List<StrategyCatalogVO> getGroupList() {
        List<StrategyCatalog> names = query().select("distinct dest_name").list();
        return names.stream().map(name -> {
            List<StrategyCatalog> list = lambdaQuery().eq(StrategyCatalog::getDestName, name.getDestName()).list();
            return new StrategyCatalogVO(name.getDestName(), list);
        }).collect(Collectors.toList());
    }
}
